******
* author: Anthony Harding (tony.harding@gatech.edu)
* Georgia Institute of Technology, School of Public Policy


* This script generates regression coefficients and tables exploring bounded exponential time trend
* Requires estout, estwide, ftools, ivreg2, reghdfe, and ivreghdfe packages

* 1st Imports Data
* 2nd Runs baseline regressions
******


clear all
set mem 1G
set matsize 10000
set maxvar 10000

* Set working directory
cd "\\Client\C$\Users\aharding6\Dropbox (GaTech)\Tobetransferred\ConsistentClimateConvergence - Copy\Replication"



*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 1. Import and prep data
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 1 - Import main datasets and merge
*-------------------------------------------------------------------------------
*******
* Temperature from MWv5
*******

import delimited data/output/MWv5/CountryTemp.csv, varnames(1) clear
drop if gmi_cntry == "REU" | gmi_cntry == "UMI"
drop if gmi_cntry == "ISR" & cntry_name != "Israel"
drop if gmi_cntry == "NOR" & cntry_name != "Norway"
reshape long temp, i(gmi_cntry) j(year)
replace temp = "." if temp == "NA"
destring temp, replace
rename temp tempMWv5
rename gmi_cntry iso
tempfile tempMWv5
save `tempMWv5'


*******
* Precipitation from MWv5
*******

import delimited data/output/MWv5/CountryPrecip.csv, varnames(1) clear
drop if gmi_cntry == "REU" | gmi_cntry == "UMI"
drop if gmi_cntry == "ISR" & cntry_name != "Israel"
drop if gmi_cntry == "NOR" & cntry_name != "Norway"
reshape long precip, i(gmi_cntry) j(year)
replace precip = "." if precip == "NA"
destring precip, replace
rename precip precMWv5
rename gmi_cntry iso
tempfile precipMWv5
save `precipMWv5'


*******
* Temperature innovation from MWv5
*******

import delimited data/output/MWv5TemperatureInnovation.csv, varnames(1) clear
keep year mwv5innovation
replace mwv5innovation = "." if mwv5innovation == "NA"
destring mwv5innovation, replace
tempfile mwv5innovation
save `mwv5innovation'


*******
* Temperature innovation from NOAA
*******

import delimited data/output/NOAATemperatureInnovation.csv, varnames(1) clear
keep year noaainnovation anomaly
replace noaainnovation = "." if noaainnovation == "NA"
destring noaainnovation, replace
tempfile noaainnovation
save `noaainnovation'

*******
* Global GDP from WDI
*******
import delimited Data/Input/GDP_Constant_wdi.csv, varnames(1) clear
local i=1960
foreach var of varlist v5-v68{
	rename `var' GDP_WLD`i'
	local i = `i'+1
}
drop indicatorname indicatorcode
reshape long GDP_WLD, i(countrycode) j(year)
keep if countrycode=="WLD"
keep year GDP_WLD
duplicates drop
tempfile GDP_WLD
save `GDP_WLD'

*******
* TotGDP from WDI
*******

import delimited Data/Input/GDP_Constant_wdi.csv, varnames(1) clear
local i=1960
foreach var of varlist v5-v68{
	rename `var' TotGDP`i'
	local i = `i'+1
}
drop indicatorname indicatorcode
reshape long TotGDP, i(countrycode) j(year)
rename countrycode iso
replace iso = "ROM" if iso == "ROU"
replace iso = "ZAR" if iso == "COD"
tempfile TotGDP
save `TotGDP'

*******
* Global GDP/capita from WDI
*******

import delimited data/input/GDPcap_wdi.csv, varnames(1) clear
local i=1960
foreach var of varlist v5-v68{
	rename `var' gdpCAP_WLD`i'
	local i = `i'+1
}
drop indicatorname indicatorcode
reshape long gdpCAP_WLD, i(countrycode) j(year)
keep if countrycode == "WLD"
duplicates drop
keep year gdpCAP_WLD
tempfile gdpCAP_WLD
save `gdpCAP_WLD'


*******
* GDP/capita from WDI
*******

import delimited data/input/GDPcap_wdi.csv, varnames(1) clear
local i=1960
foreach var of varlist v5-v68{
	rename `var' gdpCAP`i'
	local i = `i'+1
}
drop indicatorname indicatorcode
reshape long gdpCAP, i(countrycode) j(year)
rename countrycode iso
replace iso = "ROM" if iso == "ROU"
replace iso = "ZAR" if iso == "COD"
encode iso, g(iso_id)
xtset iso_id year
gen growthgdpCAP = log(gdpCAP) - log(l.gdpCAP)
drop iso_id



*******
* Merge data
*******
merge 1:1 iso year using `TotGDP'
drop _merge

merge 1:1 iso year using `tempMWv5'
drop _merge

merge 1:1 iso year using `precipMWv5'
drop _merge

merge m:1 year using `mwv5innovation'
drop _merge

merge m:1 year using `noaainnovation'
drop _merge

merge m:1 year using `GDP_WLD'
drop _merge

merge m:1 year using `gdpCAP_WLD'
drop _merge


*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 2 - Generate new useful variables
*-------------------------------------------------------------------------------
* Generate country indicator and xtset data
encode iso, g(iso_id)
xtset iso_id year


* New economic variables
gen log_gdpCAP_wdi = log(gdpCAP)
gen loggdpCAP_wdi_l = l.log_gdpCAP
rename gdpCAP gdpCAP_wdi
rename growthgdpCAP growthWDI

* Create Rich/poor dummy
preserve
drop if missing(gdpCAP_wdi)
bys iso_id: keep if _N > 19 
bys iso_id: keep if _n == 1 
xtile gdpCAP_tile = gdpCAP_wdi , nq(2)
keep iso_id gdpCAP_tile
tempfile tempxtile
save `tempxtile',replace
restore
mmerge iso_id using `tempxtile', type(n:1)
gen poor = 0
replace poor = 1 if gdpCAP_tile==1
drop _merge


* New climate variables
rename tempMWv5 temp
rename precMWv5 prec
replace prec = prec/1000 
gen temp2 = temp^2
gen prec2 = prec^2

sort iso_id year
gen dtemp = temp - l.temp
gen dprec = prec - l.prec
gen dtemp2 = temp2 - l.temp2
gen dprec2 = prec2 - l.prec2

gen poor_temp = poor*temp
gen poor_prec = poor*prec
gen poor_temp2 = poor*temp2
gen poor_prec2 = poor*prec2
gen poor_dtemp = poor*dtemp
gen poor_dprec = poor*dprec
gen poor_dtemp2 = poor*dtemp2
gen poor_dprec2 = poor*dprec2


* Generate new year variables
gen time = year-1960
gen timegr = time/(1+time)
gen timegr2 = 1/2*time^2/(1+time)
gen time2 = time^2
gen year2 = year^2
gen yeargr = year/(1+year)

gen timeex = time*exp(-1/50*(time))
gen yearex = year*exp(-1/50*(year-1960))
gen yearexdiff = yearex-l.yearex
gen timeexdiff = timeex-l.timeex

gen timeex2 = exp(-1/50*(time))
gen timegrb = time/50/(1+time/50)

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 3 - Rename and relabel key variables
*-------------------------------------------------------------------------------

* Relabel variables
label variable temp "Temp"
label variable temp2 "Temp Sq."
label variable prec "Precip"
label variable prec2 "Precip Sq."
label variable dtemp "$\Delta$Temp"
label variable dtemp2 "$\Delta$Temp Sq."
label variable dprec "$\Delta$Precip"
label variable dprec2 "$\Delta$Precip Sq."
label variable poor "poor"
label variable poor_temp "Temp*Poor"
label variable poor_prec "Precip*Poor"
label variable poor_temp2 "Temp Sq.*Poor"
label variable poor_prec2 "Precip Sq.*Poor"
*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------




*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 2. Run and Store Regressions
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 1 - Pooled SR, bounded exponential country time trend, 1 GDP lag
*-------------------------------------------------------------------------------

** Estimate for different values of tau and store estimates in csv

forvalues x = 1(1)100 {
	replace timeex = time*exp((-time)/`x')
	qui ivreghdfe growthWDI temp temp2 prec prec2 (l.log_gdpCAP_wdi = l3.		log_gdpCAP_wdi), absorb(i.year  i.iso_id i.iso_id#c.time i.iso_id#c.timeex) cluster(iso_id)
	local name = "tau`x'"
	eststo `name'
}

esttab using "data/output/TauAnalysis_SR_UpdatedData.csv",not noobs plain replace
eststo clear

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1
*-------------------------------------------------------------------------------





*-------------------------------------------------------------------------------
* Step 2 - Pooled LR, convergent country time trend, 1 GDP lag
*-------------------------------------------------------------------------------

** Estimate for different values of tau and store estimates in csv

forvalues x = 1(1)100 {
	replace timeex = time*exp((-time)/`x')
	replace timeexdiff = timeex-l.timeex
	qui ivreghdfe growthWDI dtemp dtemp2 dprec dprec2 temp temp2 prec prec2 (l.growthWDI = l3.growthWDI), absorb(i.year  i.iso_id i.iso_id#c.timeexdiff) cluster(iso_id)
	local name = "tau`x'"
	eststo `name'
}

esttab using "data/output/TauAnalysis_LR_UpdatedData.csv",not noobs plain replace
eststo clear

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2
*-------------------------------------------------------------------------------








